package com.apobates.jforum2.troll.forum.biz.impl.api;

import com.apobates.jforum2.troll.forum.entity.Board;
import com.apobates.jforum2.troll.forum.mapper.BoardVoMapperEntity;
import com.apobates.jforum2.troll.regular.ActionOperatorLog;
import com.apobates.jforum2.troll.regular.ActionOperatorLogProvider;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.regular.event.BoardActionLog;
import com.apobates.jforum2.troll.utils.core.api.ActionEventCulpritor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * SpringCloudStream RabbitMQ操作日志处理接口
 * 谁负责谁声明.若将StreamBridge提到regular模块中该如何声明呢？
 */
@Component
public class BoardActionLogStreamProvider implements ActionOperatorLogProvider {
    @Autowired
    private StreamBridge streamBridge;
    private final static Logger logger = LoggerFactory.getLogger(BoardActionLogStreamProvider.class);

    @Override
    public void send(ActionOperatorLog log) {
        logger.error("[LOG][Forum]start send");
        streamBridge.send(log.getBindingNames()+"-out-0", log.getPayload());
        logger.error("[LOG][Forum]send finish");
    }

    public void action(final ForumActionEnum action, Board board, final ActionEventCulpritor culpritor){
        BoardActionLog ba = new BoardActionLog();
        ba.setMemberId(culpritor.getMemberId());
        ba.setMemberNickname(culpritor.getMemberNickname());
        ba.setAction(action);
        ba.setBoardId(board.getId());
        ba.setBoardTitle(board.getTitle());
        ba.setRodeMember(culpritor.getMemberId());
        ba.setIpAddr(culpritor.getIpAddr());
        ba.setToken(culpritor.getToken());
        ba.setDateTime(LocalDateTime.now());
        String boardLink = BoardVoMapperEntity.produceLink.apply(board.getId(), board.getVolumesId());
        ba.setLink(boardLink);
        this.send(ba);
    }
}
